import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  // 配置路由规则，并创建路由实例
  routes: [
    { path: '/login', component: Login },
    { path: '/', redirect: '/Login' }, // 路由重定向
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [{ path: '/welcome', component: Welcome },
        { path: '/users', component: User },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params }
      ]
    }
  ]
})
// 为路由对象挂载导航守卫
router.beforeEach((to, from, next) => {
  // to 是要访问的路径
  // from 是从哪个路径访问的
  // next 是一个函数，表示放行
  // next()   next('/login)强制跳转
  if (to.path === '/login') return next()
  // 如果不是访问登陆页面，判断用户是否已登录
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})
export default router// 默认导出
